﻿using LabManagement.Data;
using OnlineLabManagement.Controllers;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineLabManagement.Areas.AdminConsole.Controllers
{
    [Authorize(Roles = "Admin")]   
    public class CategoriesController : DbControllerBase
    {

        public CategoriesController()
        {
            ViewBag.Title = "Categories";
        }
        //
        // GET: /AdminConsole/Categories/

        public ActionResult Index(int? page,int? pagesize)
        {
            var PageNumber = (page ?? 1);
            var PageSize = (pagesize ?? 5);
            var categories = this.DatabaseFactory.CategoryUtils.Categories();
            return View(categories);
        }       

        //
        // GET: /AdminConsole/Categories/Create

        public ActionResult AddNew()
        {
            return View();
        }

        //
        // POST: /AdminConsole/Categories/Create

        [HttpPost]
        public ActionResult AddNew(Category category)
        {
            try
            {
                // TODO: Add insert logic here
                category.CategoryId = Guid.NewGuid();
                category.CreationDate = DateTime.UtcNow;
                if (ModelState.IsValid)
                {
                    this.DataBridge.Entry(category).State = EntityState.Added;
                    this.DataBridge.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminConsole/Categories/Edit/5

        public ActionResult Edit(Guid id)
        {
            var category = this.DataBridge.Categories.Find(id);
            return View(category);
        }

        //
        // POST: /AdminConsole/Categories/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, Category category)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var categorySaved = this.DataBridge.Categories.Find(id);
                    if (categorySaved != null)
                    {
                        categorySaved.CategoryName = category.CategoryName;
                        categorySaved.CategoryType = category.CategoryType;
                        categorySaved.Active = category.Active;
                        this.DataBridge.Entry(categorySaved).State = EntityState.Modified;
                        this.DataBridge.SaveChanges();
                    }                   
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // POST: /AdminConsole/Categories/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id)
        {
            try
            {
                var deletedCategory = this.DataBridge.Categories.Find(id);
                if (deletedCategory != null)
                {
                    this.DataBridge.Categories.Remove(deletedCategory);
                    this.DataBridge.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
